package com.atguigu.tingshu.account.receiver;


import com.atguigu.tingshu.account.mapper.RechargeInfoMapper;
import com.atguigu.tingshu.account.service.RechargeInfoService;
import com.atguigu.tingshu.account.service.UserAccountService;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.rabbit.constant.MqConst;
import com.atguigu.tingshu.model.account.RechargeInfo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.io.IOException;


@Component
public class AccountReceiver {
    @Autowired
    private RechargeInfoMapper rechargeInfoMapper;
    @Autowired
    private RechargeInfoService rechargeInfoService;

    private static final Logger log = LoggerFactory.getLogger(AccountReceiver.class);

    /**
     * 充值成功
     * @param orderNo
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MqConst.QUEUE_RECHARGE_PAY_SUCCESS,durable = "true",autoDelete="false"),
            exchange = @Exchange(value = MqConst.EXCHANGE_ORDER),
            key = MqConst.ROUTING_RECHARGE_PAY_SUCCESS
    ))
    public void rechargePaySuccess(String orderNo, Message message, Channel channel){
        //判断
        if(!StringUtils.isEmpty(orderNo)){
            //修改充值状态，recharge_info,将充值状态改为已支付
            rechargeInfoService.updateRechargeInfo(orderNo);
        }


        //手动确认
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }


    /**
     * 新用户注册时会增加100余额
     */
    @Autowired
    private UserAccountService userAccountService;
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MqConst.QUEUE_USER_REGISTER,durable = "true",autoDelete="false"),
            exchange = @Exchange(value = MqConst.EXCHANGE_ALBUM),//durable和autoDelete都有默认的，不用设置
            key = MqConst.ROUTING_USER_REGISTER
    ))
    public void userRegister(Long userId, Message message, Channel channel) {
        System.out.println("用户注册成功，用户id："+userId);
        try {
            if(null != userId){
                //说明获取到了用户id，给用户充钱
                userAccountService.userRegister(userId);
            }
        } catch (Exception e) {

            throw new RuntimeException(e);
        }
        try {
            //第一个参数 message.getMessageProperties().getDeliveryTag() 获取当前消息的唯一标识（投递标签）
            //第二个参数 false 表示只确认当前这条消息，而不是批量确认
            //用于通知 RabbitMQ 消息已被成功处理，可以从队列中移除
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            throw new RuntimeException(e);

        }

    }
}
